﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Alvin.Common.Utils
{
    /// <summary>
    /// 枚举的显示名称
    /// </summary>
    [global::System.AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
    public sealed class EnumShowNameAttribute : Attribute
    {
        private readonly string _showName;

        /// <summary>
        /// 显示名称
        /// </summary>
        public string ShowName
        {
            get
            {
                return this._showName;
            }
        }

        /// <summary>
        /// 构造枚举的显示名称
        /// </summary>
        /// <param name="showName">显示名称</param>
        public EnumShowNameAttribute(string showName)
        {
            this._showName = showName;
            
        }
    }
    /// <summary>
    /// 枚举工具类
    /// </summary>
    public sealed class EnumUtil
    {
        /// <summary>
        /// 将枚举类型的值转成字典
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static Dictionary<int, String> EnumToIntDictionary(Type enumType)
        {
            var dictionary = new Dictionary<int, string>();
            foreach (int i in Enum.GetValues(enumType))
            {
                var name = Enum.GetName(enumType, i);

                //取显示名称
                var showName = string.Empty;
                var atts = enumType.GetField(name).GetCustomAttributes(typeof(EnumShowNameAttribute), false);
                if (atts.Length > 0) showName = ((EnumShowNameAttribute)atts[0]).ShowName;

                dictionary.Add(i, string.IsNullOrEmpty(showName) ? name : showName);
            }
            return dictionary;
        }

        /// <summary>
        /// 将枚举类型的值转成字典
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static Dictionary<int, String> EnumToIntDictionary<T>()
        {
            return EnumToIntDictionary(typeof(T));
        }

        /// <summary>
        /// 将枚举类型的值转成字典
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static Dictionary<String, String> EnumToStringDictionary<T>()
        {
            return EnumToStringDictionary(typeof(T));
        }

        /// <summary>
        /// 将枚举类型的值转成字典
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static Dictionary<string, string> EnumToStringDictionary(Type enumType)
        {
            var dictionary = new Dictionary<string, string>();
            foreach (int i in Enum.GetValues(enumType))
            {
                var name = Enum.GetName(enumType, i);

                //取显示名称
                var showName = string.Empty;
                var atts = enumType.GetField(name).GetCustomAttributes(typeof(EnumShowNameAttribute), false);
                if (atts.Length > 0) showName = ((EnumShowNameAttribute)atts[0]).ShowName;

                dictionary.Add(name, string.IsNullOrEmpty(showName) ? name : showName);
            }
            return dictionary;
        }
    }
}
